"""
Nacos 配置
"""

import traceback

import requests
import yaml

from config import settings

CONFIG_URL: str = (
    f"{{url}}/nacos/v2/cs/config?"
    f"username={{username}}&"
    f"password={{password}}&"
    f"namespaceId={{namespace}}&"
    f"group={{group}}&"
    f"dataId={{data_id}}"
)

config_dict: dict = {
    "url": settings.NACOS.URL,
    "username": settings.NACOS.USERNAME,
    "password": settings.NACOS.PASSWORD,
    "namespace": settings.NACOS.NAMESPACE,
    "data_id": settings.NACOS.DATA_ID,
    "group": settings.NACOS.GROUP,
}


def get_config() -> dict:
    """
    获取配置

    - Doc: https://nacos.io/docs/latest/guide/user/open-api/#1.1
    """

    url: str = CONFIG_URL.format(**config_dict)

    try:
        res = requests.get(url)
        print(url)
        config = yaml.load(res.text, Loader=yaml.FullLoader)

    except:  # noqa
        print(f"[Nacos]获取配置失败{traceback.format_exc()}")

        return {}

    return config


if __name__ == '__main__':
    from devtools import debug

    debug(get_config())
